import java.util.concurrent.TimeUnit;

//创建两个线程，一个调用wait()，一个调用notify()
public class Demo_501 {
    public static void main(String[] args) {
        //定义一个锁对象
        Object locker = new Object();

        //创建调用wait()的线程
        Thread t1 = new Thread(()->{
            while (true){

                synchronized (locker){
                    System.out.println("调用wait()之前...");
                    //执行线程逻辑
                    //如果没有满足线程所需的数据，那么就等待
                    try {
                        locker.wait();
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    System.out.println("wait()唤醒之后...");
                    System.out.println("=================");
                }
            }
        });

        Thread t2 = new Thread(()->{
            while (true){
                System.out.println("notifyAll()之前...");
                //同等待的锁对象进行唤醒
                synchronized (locker){
                    locker.notifyAll();
                }
                System.out.println("notifyAll()之后...");
                try {
                    TimeUnit.SECONDS.sleep(1);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        });

        //启动线程
        t1.start();
        t2.start();
    }
}
